<?php

namespace app\webapi\controller;

use think\facade\Db;
use think\facade\Request;

class ApiUserParcel extends ApiBaseController
{
    public $user_info = null;

    public function __construct()
    {
        $this->user_info = $this->checkToken();
        if (!preg_match('/^\d+$/', $this->user_info['id'])) {
            $this->result_error('用户id出错');
        }
    }

    /** 获取快递列表 */
    public function getList()
    {
        $sql_result = Db::name('user_parcel')->where('uid', $this->user_info['id'])->where('stats <> 1')->order('update_time', 'desc')->select()->toArray();
        $this->result_ok('ok', $sql_result);
    }

    /** 请求发货 */
    public function needParcel()
    {
        $order_id = (int)Request::param('order_id');
        if (!($order_id > 0)) {
            $this->result_error('订单id错误');
        }
        $parcel = "";
        $parcel_state = 0;
        $sql_result = Db::name('user_parcel')->insertGetId([
            'uid'=>$this->user_info['id'],
            'order_id'=>$order_id,
            'parcel'=>$parcel,
            'parcel_state'=>$parcel_state
        ]);
        if($sql_result){
            $this->result_ok('申请成功');
        }else{
            $this->result_error("申请失败");
        }
    }

    /** 确认收货 */
    public function ack()
    {
        $id = (int)Request::param('id');
        if (!($id > 0)) {
            $this->result_error('确认收货失败');
        }
        $sql_result = Db::name('user_parcel')->where('id',$id)->where('uid',$this->user_info['id'])->update(['parcel_state'=>1]);
        if($sql_result){
            $this->result_ok('ok');
        }else{
            $this->result_error("确认收货失败");
        }

    }

    /** 删除收货信息 */
    public function deletesh()
    {
        $id = (int)Request::param('id');
        if (!($id > 0)) {
            $this->result_error('订单id错误');
        }
        $sql_result = Db::name('user_parcel')->where('id',$id)->where('uid',$this->user_info['id'])->update(['state'=>-1]);
        if($sql_result){
            $this->result_ok('删除成功');
        }else{
            $this->result_error("删除失败");
        }
    }

}